package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("C端-用户地址簿接口")
@Slf4j
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查询当前登录用户的所有地址信息
     *
     * @return
     */
    @ApiOperation("查询当前登录用户的所有地址信息")
    @GetMapping("/list")
    public Result<List<AddressBook>> list() {
        log.info("查询当前登录用户的所有地址信息");
        List<AddressBook> addressBookList = addressBookService.list();
        return Result.success(addressBookList);
    }

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @ApiOperation("新增地址")
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook) {
        log.info("新增地址相关信息，{}", addressBook);
        addressBookService.save(addressBook);
        return Result.success();
    }

    /**
     * 根据地址id查询地址
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> getByid(@PathVariable Long id) {
        log.info("根据地址id查询地址，id:{}", id);
        AddressBook addressBook = addressBookService.getByid(id);
        return Result.success(addressBook);
    }


    /**
     * 根据地址id修改地址
     *
     * @param addressBook
     * @return
     */
    @ApiOperation("根据地址id查询地址")
    @PutMapping
    public Result updateById(@RequestBody AddressBook addressBook) {
        log.info("根据地址id查询地址，相关信息：{}", addressBook);
        addressBookService.updateById(addressBook);
        return Result.success();
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook) {
        log.info("设置默认地址，地址相关信息:{}", addressBook);
        addressBookService.setDefault(addressBook);
        return Result.success();
    }


    /**
     * 根据id删除地址
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteById(Long id) {
        log.info("需要删除的地址id，{}", id);
        addressBookService.deleteByid(id);
        return Result.success();
    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddress() {
        log.info("查询默认地址");
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(1);
        AddressBook address = addressBookService.getDefaultAddress(addressBook);
        if (address.getIsDefault()==1){
            return Result.success(address);
        }else {
            return Result.error("没有查询到默认地址！！！");
        }

    }

}
